<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
<!--                <el-form-item label="分类ID" prop="categoryId">-->
<!--                    <el-input class="w-[280px]" v-model="queryParams.categoryId" />-->
<!--                </el-form-item>-->
                <el-form-item label="产品名称" prop="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" />
                </el-form-item>
<!--                <el-form-item label="搜索关键词" prop="keywords">-->
<!--                    <el-input class="w-[280px]" v-model="queryParams.keywords" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="是否热门：0-否，1-是" prop="ishot">-->
<!--                    <el-input class="w-[280px]" v-model="queryParams.isHot" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="是否最新：0-否，1-是" prop="isNew">-->
<!--                    <el-input class="w-[280px]" v-model="queryParams.isNew" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="排序" prop="sort">-->
<!--                    <el-input class="w-[280px]" v-model="queryParams.sort" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="状态：0-下架，1-上架" prop="status">-->
<!--                    <el-select-->
<!--                        v-model="queryParams.status"-->
<!--                        class="w-[280px]"-->
<!--                        clearable-->
<!--                    >-->
<!--                        <el-option label="请选择字典生成" value="" />-->
<!--                    </el-select>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="浏览量" prop="views">-->
<!--                    <el-input class="w-[280px]" v-model="queryParams.views" />-->
<!--                </el-form-item>-->
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['product:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="分类ID" prop="categoryId" min-width="100" />
                <el-table-column label="产品名称" prop="name" min-width="100" />
                <el-table-column label="产品描述" prop="description" min-width="100" />
                <el-table-column label="主图" prop="image" min-width="100">
                    <template #default="{ row }">
                        <image-contain
                            :width="40"
                            :height="40"
                            :src="row.image"
                            :preview-src-list="[row.image]"
                            preview-teleported
                            hide-on-click-modal
                        />
                    </template>
                </el-table-column>
<!--                <el-table-column label="产品图片集，JSON格式" prop="images" min-width="100" />-->
<!--                <el-table-column label="产品详情" prop="details" min-width="100" />-->
<!--                <el-table-column label="搜索关键词" prop="keywords" min-width="100" />-->
<!--                <el-table-column label="是否热门：0-否，1-是" prop="isHot" min-width="100" />-->
<!--                <el-table-column label="是否最新：0-否，1-是" prop="isNew" min-width="100" />-->
<!--                <el-table-column label="排序" prop="sort" min-width="100" />-->
<!--                <el-table-column label="状态：0-下架，1-上架" prop="status" min-width="100" />-->
<!--                <el-table-column label="浏览量" prop="views" min-width="100" />-->
                <el-table-column label="创建时间" prop="createTime" min-width="100" />
                <el-table-column label="更新时间" prop="updateTime" min-width="100" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['product:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['product:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="product">
import { productDelete, productLists } from '@/api/product'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    categoryId: '',
    name: '',
    description: '',
    image: '',
    images: '',
    details: '',
    keywords: '',
    isHot: '',
    isNew: '',
    sort: '',
    status: '',
    views: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: productLists,
    params: queryParams
})



const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await productDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
